package com.springboot.love.controller;

import com.alibaba.fastjson.JSONObject;
import com.springboot.love.entity.FriendLink;
import com.springboot.love.service.LinkService;
import com.springboot.love.utils.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * (link)控制层
 */
@Controller
@RequestMapping("link")
@Slf4j
public class LinkController {
    /**
     * 服务对象
     */
    @Resource
    private LinkService linkService;


    @RequestMapping("/linkAll")
    public String linkAll(){
        return "admin/listLink";
    }

    /**
     * 查询所有友情
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/linkList")
    @ResponseBody
    public String linkList(String page, String limit){
        PageUtil pageUtil=new PageUtil(Integer.parseInt(page),Integer.parseInt(limit));
        List<FriendLink> linkList = linkService.queryAllByLimit(pageUtil);
        int linkTotal = linkService.queryTotalByTime();
        JSONObject json = new JSONObject();
        json.put("code",0);
        json.put("data",linkList);
        json.put("count", linkTotal);
        json.put("status", 200);
        return json.toString();
    }

    /**
     * 添加公告
     * @return
     */

    @RequestMapping("/linkadd")
    public String linkadd(){
        return "admin/addLink";
    }
    @RequestMapping("/addlink")
    @ResponseBody
    public String addlink(String linkName,String linkLink){
        return linkService.insertLink(linkName,linkLink);
    }

    /**
     * 更新友情
     * @return
     */
    @RequestMapping("/updatelink")
    @ResponseBody
    public String updatelink(String linkId,String linkName,String linkLink){
        return linkService.updateLink(linkId,linkName,linkLink);
    }

    /**
     * 单个删除友情
     * @return
     */
    @RequestMapping("/deleteLink")
    @ResponseBody
    public String deleteLink(String linkId){
        return linkService.deleteByIdLink(linkId);
    }

    /**
     * 批量删除友情
     * @param delete
     * @return
     */
    @RequestMapping("/deleteLinks")
    @ResponseBody
    public String  deleteLinks(String delete){
        return linkService.deletesLink(delete);
    }


}